#include "TextureManager.h"
#include "ReportError.h"
#include "LoadScreen.h"
#include "File.h"
#include <assert.h>

TextureManager::TextureManager()
{
	m_it = m_textures.end();
}

bool TextureManager::Load(const std::string& fileName)
{
	File f;
	if(!f.OpenRead(fileName))
	{
		ReportError("Could not open texturepaths\n");
		return false;
	}

	std::string str;
	
	
	while(!f.EndOf())
	{
		//Format is:
		//filepath
		if(!f.GetString(&str))
		{
			ReportError("texture filepath not found\n");
		}


		if(!LoadTex(str))
		{
			ReportError("Tex wont load");
		}
		
	}

	return true;
}

Texture* TextureManager::GetTexture(const std::string& path)
{
	return m_textures.find(path)->second;
}

bool TextureManager::LoadTex(const std::string& path)
{
	//static int i = 0;

	Texture* t = new Texture;

	if(!t->Load("Textures/" + path))
	{
		ReportError("Texture load failed" + __LINE__);
		assert(0);
	}
	m_textures.insert(std::pair<std::string, Texture*> (path, t));

	static int i = 0;
	i++;
	if(i > 4)
	{
		TheLoadScreen::Instance()->IncrementDraw();
	}
	
	return true;

}

void TextureManager::DrawTexture(const std::string& texName)
{
	m_it = m_textures.find(texName);
	assert(m_it != m_textures.end());
	(*m_it).second->Draw();
}